package cn.daniellee.plugin.lm.storage;

import cn.daniellee.plugin.lm.LotteryMaster;
import cn.daniellee.plugin.lm.model.Prize;
import cn.daniellee.plugin.lm.model.PrizePool;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class JsonStorage extends Storage {

    private File prizeFile = new File(LotteryMaster.getInstance().getDataFolder(), "prize.json");

//    private FileConfiguration prizeData = new YamlConfiguration();

    @Override
    public boolean initialize() {
        try {
            if (!prizeFile.exists()) prizeFile.createNewFile();
//            prizeData.load(prizeFile);
        } catch (Exception e) {
            e.printStackTrace();
            LotteryMaster.getInstance().getLogger().info(" ");
            LotteryMaster.getInstance().getLogger().info("[LotteryPlus]An error occurred in prize file load.");
            LotteryMaster.getInstance().getLogger().info(" ");
            return false;
        }
        return refreshCache();
    }

    @Override
    public boolean refreshCache() {
        BufferedReader reader = null;
        try {
            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new FileReader(prizeFile));
            String temp;
            while ((temp = reader.readLine()) != null) sb.append(temp);
            prizePools.clear();
            List<PrizePool> jsonPools = new Gson().fromJson(sb.toString(), new TypeToken<ArrayList<PrizePool>>(){}.getType());
            if (jsonPools != null) prizePools.addAll(jsonPools);
        } catch (Exception e) {
            e.printStackTrace();
            LotteryMaster.getInstance().getLogger().info(" ");
            LotteryMaster.getInstance().getLogger().info("[LotteryPlus]An error occurred in prize file load.");
            LotteryMaster.getInstance().getLogger().info(" ");
            return false;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignored) { }
            }
        }
        return true;

//        Set<String> ids = prizeData.getKeys(false);
//        for (String id : ids) {
//            PrizePool prizePool = new PrizePool();
//            prizePool.setId(id);
//            prizePool.setName(prizeData.getString(id + ".name", "&e&lUnnamed"));
//            prizePool.setOpen(prizeData.getBoolean(id + ".open", false));
//            prizePool.setPriceType(prizeData.getString(id + ".price-type", "MONEY"));
//            prizePool.setPrice(prizeData.getDouble(id + ".price", 1000D));
//            prizePool.setUnlimited(prizeData.getBoolean(id + ".unlimited", false));
//            List<String> prizesStr = prizeData.getStringList(id + ".prizes");
//            List<Prize> prizes = new ArrayList<>();
//            for (String prizeStr : prizesStr) {
//                prizes.add(new Gson().fromJson(prizeStr, Prize.class));
//            }
//            prizePool.setPrizes(buildingData.getBoolean(id + ".reviewed", false));
//            allBuilding.add(building);
//        }
    }

    @Override
    public void addPrizePool(PrizePool prizePool) {
        prizePools.add(prizePool);
        saveJson();
    }

    private void saveJson() {
        Gson gson = new Gson();
        JsonWriter writer = null;
        try {
            writer = gson.newJsonWriter(new BufferedWriter(new FileWriter(prizeFile)));
            writer.setIndent("  ");
            writer.beginArray();
            for (PrizePool prizePool : prizePools) {
                gson.toJson(prizePool, PrizePool.class, writer);
            }
            writer.endArray();
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ignored) { }
            }
        }
    }

}
